package api

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"github.com/astaxie/beego"
	"strconv"
)

type HomeworkController struct {
	BaseController
}

// 家长作业通知
func (c *HomeworkController) ListByParent() {
	var res *utils.ResJsonStruct

	count, _ := beego.AppConfig.Int("loadcount")
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}

	userId := c.GetSession("miniUserId")
	p := map[string]interface{}{}
	p["id"] = userId
	user := service.GetUserInfo(p)

	var studentIds []int64
	p = map[string]interface{}{}
	p["status"] = 1
	p["familySn"] = user.FamilySn
	_, studentList := service.GetStudentList(p)
	for _, student := range studentList {
		studentIds = append(studentIds, student.Id)
	}

	var homeworkList []*models.HomeworkNotice
	if len(studentIds) > 0 {
		p = map[string]interface{}{}
		p["relation"] = true
		p["api"] = true
		p["studentIds"] = studentIds
		p["curPage"] = curPage
		p["perCount"] = count
		_, homeworkList = service.GetHomeworkNoticeList(p)
	}

	res = utils.ResJson(0, "成功获取数据", homeworkList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 老师作业历史
func (c *HomeworkController) ListByTeacher() {
	var res *utils.ResJsonStruct
	userId := c.GetSession("miniUserId")

	count, _ := beego.AppConfig.Int("loadcount")
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}

	p := map[string]interface{}{}
	p["relation"] = true
	p["api"] = true
	p["userId"] = userId
	p["curPage"] = curPage
	p["perCount"] = count
	_, homeworkList := service.GetHomeworkList(p)

	res = utils.ResJson(0, "成功获取数据", homeworkList)
	c.Data["json"] = res
	c.ServeJSON()
	return
}

